package com.focus.xyz.union.cases.frontend;

import com.focus.xyz.core.common.annotation.Task;
import com.focus.xyz.core.common.webdriver.DriverFactory;
import com.focus.xyz.union.action.KeyWordAction;
import com.focus.xyz.core.common.listeners.TC;
import com.focus.xyz.core.utils.Constants;
import com.focus.xyz.core.utils.ExcelUtils;
import com.focus.xyz.core.utils.ProcessUtil;
import com.focus.xyz.core.utils.prop.ConfigPropInfo;
import com.focus.xyz.union.data.InsDataBean;
import com.focus.xyz.union.data.InsDataService;
import com.focus.xyz.union.pages.frontend.FrontMenuList;
import com.focus.xyz.union.pages.frontend.tables.OrderTable;
import com.focus.xyz.union.pages.frontend.tables.TradeRecordTable;
import org.openqa.selenium.WebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.*;

/**
 * Created by chenmeng on 2016/5/23.
 */
public class PayTest extends TC {
    private static transient Logger logger = LoggerFactory.getLogger(PayTest.class);

    private WebDriver driver;
    private KeyWordAction action;
    private ExcelUtils eu;

    @BeforeClass
    public void beforeClass() throws Exception{
        eu = new ExcelUtils(ConfigPropInfo.TEST_DATA_DIR + ConfigPropInfo.TEST_UNION_DATA_FILE);
        action.DelUnionFrontCookie();
    }


    @Parameters({"nodeUrl","browser","userName","password"})
    @BeforeMethod(alwaysRun = true)
    public void setUp(String nodeUrl,String browser,String userName,String password) throws Exception {
        driver = DriverFactory.getDriver(nodeUrl).getBrowser(browser);
        super.init(driver);
        action = new KeyWordAction(driver);
        action.LoginFront(userName,password);
    }


    @Parameters({"browser"})
    @AfterMethod(alwaysRun = true)
    public void tearDown(String browser) {
        try{
            super.quit();
            driver.quit();
        }catch (Exception e){
            ProcessUtil.closeBrowser(browser);
        }finally {

        }
    }


    @Task(taskName = {"DEL_UNION_ORDER_APP.sql"},paras={"param0=cimon007"})
    @Parameters({"payPass","insDataIdx"})
    @Test(description = "充值账户支付")
    public void ACCT_PAY(String payPass,String insDataIdx) throws Exception{
        InsDataBean bean = InsDataService.getInsDataById(insDataIdx);
        action.OpenToubaoPage(bean.getProdName());
        action.CommonToubao(bean);
        action.pause(3000l);
        action.AcctPay(bean.getExpectedPrice(),payPass);
        action.assertStringTrue("支付成功");
    }


    @Test(description = "订单详情中显示价格校验")
    @Parameters({"userName","insDataIdx"})
    public void CHECK_ORDER_INFO(String userName,String insDataIdx) throws Exception{
        FrontMenuList menuList = new FrontMenuList(driver,action);
        menuList.openMenu(Constants.U_F_ORD_MENU);
        OrderTable ot = new OrderTable(driver,action);
        InsDataBean bean = InsDataService.getInsDataById(insDataIdx);
        if(!ot.getAppStatus(userName).contains("已承保")){
            action.pause(10000l);
            menuList.openMenu(Constants.U_F_ORD_MENU);
            if(!ot.getAppStatus(userName).contains("核保中")){
                action.fail("承保失败");
            }
        }
        if(bean.getExpectedPrice().equals(ot.getAppFee())){
            action.fail("实际显示保费金额与预期保费金额不一致");
        }
        ot.linkOrderInfo(userName);
    }

    @Parameters({"settleType","insDataIdx","feeRate"})
    @Test(description = "交易记录校验，收取的服务费校验")
    public void CHECK_BILL_RECORD(String settleType,String insDataIdx,String feeRate) throws Exception{
        FrontMenuList menuList = new FrontMenuList(driver,action);
        menuList.openMenu(Constants.U_F_TRADE_MENU);
        TradeRecordTable trt = new TradeRecordTable(driver,action);
        InsDataBean bean = InsDataService.getInsDataById(insDataIdx);
        if(trt.getTradeCount() != 2){
            action.fail("显示交易记录数量与实际不符");
        }
        if(trt.checkStatus() == false){
            action.fail("保单状态错误");
        }
        if(trt.checkFeeRate(settleType,bean.getExpectedPrice(),bean.getInsuredId(),feeRate) == false){
            action.fail("显示服务费与预期服务费不符");
        }
    }
}
